package com.zimmur.app.mocolink.background.util;

import java.net.URLDecoder;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zimmur.app.mocolink.domain.util.DateUtils;


public class SecretUtil {
	private static Logger log=LoggerFactory.getLogger(SecretUtil.class);
	
	private static SecretUtil instance= null;
	private final static String spliteSybole="&&"; 
	
	////私有的默认构造
	private SecretUtil(){
	}
	
	public synchronized static SecretUtil Instance(){
		if (instance == null) {  
			instance = new SecretUtil();
		}
		return instance;
	}
	
	/**
	 * 返回加密后的字符串
	 * @return
	 */
	public String encode(int userId){
		int expaireDate=DateUtils.getAfterdayUnixTime(7);
		String strRet = "";
		try {
			strRet = DESTools.AES_Encrypt(userId+spliteSybole+String.valueOf(expaireDate));
			strRet = URLEncoder.encode(strRet, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strRet; 
	}
	
	private static String decode(String token) {
		String strDes = "";
		try{
			if(token.indexOf("%")>-1){
				token = URLDecoder.decode(token, "utf-8");
			}
			
			strDes = DESTools.AES_Decrypt(token);
		}catch(Exception er){
			log.error(er.getMessage());
		}
		return strDes;
	}
	
	/**
	 * 根据Token返回用户ID
	 * @param token
	 * @return
	 */
	public int getUserId(String token){
		int userId=0;
		try{
			String strDes = decode(token);
			if(strDes != null){
				userId = Integer.parseInt(strDes.split(spliteSybole)[0]);
			}
		}catch(Exception er){
			er.printStackTrace();
		}
		return userId;
	}
	/**
	 * 根据Token返回时间
	 * @param token
	 * @return
	 */
	public int getRequestTime(String token){
		int time=0;
		try{
			String strDes = decode(token);
			if(strDes != null){
				time = Integer.parseInt(strDes.split(spliteSybole)[1]);
			}
		}catch(Exception er){
			er.printStackTrace();
		}
		return time;
	}
	/**
	 * 判斷token是否過期
	 * @param token
	 * @return true:已過期 false:有效期內
	 */
	public static boolean isTokenExpaire(String token){
		boolean bRet=true;
		try{
			if(token == null || token.equals("")){
				return bRet;
			}
			String strDes = decode(token);
			if (strDes!=null && !strDes.equals("") && strDes.indexOf(spliteSybole) > -1) {
				String[] strArr = strDes.split(spliteSybole);
				if (strArr.length == 2) {
					int unixTime = Integer.parseInt(strArr[1]);
					if (!DateUtils.isExpiredDay(DateUtils.getDateByUnixTime(unixTime))) {
						bRet = false;
					}
				}
			}
		}catch(Exception er){
			log.error(er.getMessage());
		}
		return bRet;
	}
	
	public static void main(String[] args) {
		int userId = 123456;	
		String enString=SecretUtil.Instance().encode(userId);
		System.out.println("enString:"+enString);
		
		String descText = "9ptLoSqpN90hOV5EZcIXG%2B%2B3R9PFB3nKYqliOLLv7Es%3D";
		long deUserId = SecretUtil.Instance().getUserId(descText);
		System.out.println("deUserId:"+deUserId);
		System.out.println("isTokenExpaire:"+SecretUtil.Instance().isTokenExpaire(descText));
	}
	
}
